import requests
from django.conf import settings

class WorkWachat:

    access_token = ''

    def __init__(self):
        self.corpid = settings.WORK_WECHAT_KEY['corpid']
        self.corpsecret = settings.WORK_WECHAT_KEY['corpsecret']
        self.getAccessToken()

    def getAccessToken(self):
        url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + self.corpid + '&corpsecret=' + self.corpsecret
        r = requests.get(url)
        self.access_token = r.json()['access_token']

    def getUserInfo(self, code):
        url = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=' + self.access_token + '&code=' + code
        r = requests.get(url)
        return r.json()

    def getUserDetail(self, userid):
        url = 'https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=' + self.access_token + '&userid=' + userid
        r = requests.get(url)
        return r.json()